/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

/**
 *
 * @author alessandro
 */
import game.BOT;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.icefaces.application.PortableRenderer;
import org.icefaces.application.PushRenderer;
import other.Costants;
import other.TextModel;

@ManagedBean(name = "messageBean")
@ApplicationScoped
public class MessageBean implements Serializable {

    private static final int MAX_SIZE = 40;
    private List<TextModel> textList = new ArrayList<TextModel>(0);
    private ArrayList<String> tables = new ArrayList<String>();
    private ArrayList<ArrayList<TextModel>> messages = new ArrayList<ArrayList<TextModel>>();

    public MessageBean() {
    }

    public ArrayList<ArrayList<TextModel>> getMessages() {
        return messages;
    }

    public void setMessages(ArrayList<ArrayList<TextModel>> messages) {
        this.messages = messages;
    }

    public ArrayList<String> getTables() {
        return tables;
    }

    public void setTables(ArrayList<String> tables) {
        this.tables = tables;
    }

    public List<TextModel> getTextList() {
        return textList;
    }

    public void setTextList(List<TextModel> textList) {
        this.textList = textList;
    }

    public void addToList(String user, String message) {
        if (!(message.equals(""))) {
            textList.add(makeTextModel(user, message));
        }

        if (textList.size() > MAX_SIZE) {

            textList.remove(0);
            textList.remove(1);
        }
    }

    public void addToList(String user, final String message, String table) {
        if (!(message.equals(""))) {
            final int indexTable = tables.indexOf(table);
            if (indexTable >= 0) {
                messages.get(indexTable).add(makeTextModel(user, message));

                final PortableRenderer pr = PushRenderer.getPortableRenderer();

                 //bot risponde alla chat
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String messageBot = BOT.talk(message);
                        messages.get(indexTable).add(makeTextModel(Costants.BOTNAME + (int) (3 * Math.random()), messageBot));
                        pr.render(Costants.PUSH_TABLE);
                    }
                });
                t.start();

                if (messages.get(indexTable).size() > MAX_SIZE) {
                    messages.get(indexTable).remove(0);
                    messages.get(indexTable).remove(1);
                }
                PushRenderer.render(Costants.PUSH_TABLE);


            }
        }


    }
    
    public void addTable(String name) {
        if (!(tables.indexOf(name) >= 0)) {
            tables.add(name);
            messages.add(new ArrayList<TextModel>());
        }
    }

    public void removeTable(String name){
        int index = tables.indexOf(name);
        if (index >= 0) {
            tables.remove(index);
            messages.remove(index);
        }
    }

    private TextModel makeTextModel(String user, String message) {
        return new TextModel(user + ": " + message, message);
    }

    public ArrayList<TextModel> messagesChatOfTable(String name) {
        int index = tables.indexOf(name);
        ArrayList<TextModel> output = new ArrayList<TextModel>();
        if(index >= 0) {
            output = messages.get(index);
        }
        return output;
    }
}
